#include "stdafx.h"
#include "EventConditon.h"
#include "Factory.h"
#include "Utility.h"
#include "ReactorDef.h"
#include "InstanceTouch.h"
/*
static IDFactory< EventConditionBase>g_EventConditonFactory;

#define REGISTER_EVC(EVC) g_EventConditonFactory.Register<EVC>(EVC::MatchEventID);
struct EventConditon_InstanceTouch
{
	EventConditon_InstanceTouch()
	{
		REGISTER_EVC(EVC_OnCardBeOnBattle);
		REGISTER_EVC(EVC_OnRoundBegin);
		REGISTER_EVC(EVC_DoCastSkill);
		REGISTER_EVC(EVC_ActOver);
		REGISTER_EVC(EVC_OnRoundOver);
		REGISTER_EVC(EVC_AfterTakenDamage);
		REGISTER_EVC(EVC_AfterMeleeAttackOneTarget);
		REGISTER_EVC(EVC_BeEstimated);
		REGISTER_EVC(EVC_BeCaculateDamageLimit);
		REGISTER_EVC(EVC_BeCaculateDamageReduce);
		REGISTER_EVC(EVC_OnCardDie);
		REGISTER_EVC(EVC_OnCardLeaveBattle);
		REGISTER_EVC(EVC_BeforeCastAffectToTargets);
		REGISTER_EVC(EVC_BeEstimatedAddBuffTo);
		REGISTER_EVC(EVC_OnCardMoveToTomb);
	}

}__eventconditon_instancregister;
*/

DECL_INSTANCE_TOUCH(ETouch_EvntConditon);

#define REGISTER_EVENTCONDITON_MGR(EventConditon) \
	GlobalClassManager::Instance().RegisterClass(EventConditon::s_GetReflection(),EventConditon::MatchEventID,eDefGroup_EventConditons);\
	GlobalDefManager::Instance().RegisterDefManger( &DefMangerTpl<EventConditon>::Instance());

struct EventContitionDefReflectionRegister
{
	EventContitionDefReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(EVC_AfterTakenDamage,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(EVC_AfterTakenDamage,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(EVC_AfterTakenDamage,m_vDmgTypeIncludes,std::vector<int32>()).Editor_SetAsEnumationSet<EDamageType>();
		REGISTER_REFLECTION_MEMBER(EVC_AfterTakenDamage,m_vDmgTypeExcludes,std::vector<int32>()).Editor_SetAsEnumationSet<EDamageType>();

		REGISTER_REFLECTION_MEMBER(EVC_AfterMeleeAttackOneTarget,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(EVC_AfterMeleeAttackOneTarget,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(EVC_AfterMeleeAttackOneTarget,m_tiFirstAttackOrNot,eTriple_Null).Editor_SetAsEnumation<ETriple>();
		REGISTER_REFLECTION_MEMBER(EVC_AfterMeleeAttackOneTarget,m_iHpChangeGreaterThan,INVALID_ID).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(EVC_AfterMeleeAttackOneTarget,m_iDamageGreaterThan,INVALID_ID).Editor_SetAsRaw();

		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageLimit,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageLimit,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageLimit,m_vDmgTypeIncludes,std::vector<int32>()).Editor_SetAsEnumationSet<EDamageType>();
		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageLimit,m_vDmgTypeExcludes,std::vector<int32>()).Editor_SetAsEnumationSet<EDamageType>();
		

		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageReduce,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageReduce,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageReduce,m_vDmgTypeIncludes,std::vector<int32>()).Editor_SetAsEnumationSet<EDamageType>();
		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageReduce,m_vDmgTypeExcludes,std::vector<int32>()).Editor_SetAsEnumationSet<EDamageType>();
		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageReduce,m_eDmgCompare,eCompare_Null).Editor_SetAsEnumation<ECompare>();
		REGISTER_REFLECTION_MEMBER(EVC_BeCaculateDamageReduce,m_iDmgCompareValue,0).Editor_SetAsRaw();

		REGISTER_REFLECTION_MEMBER(EVC_BeEstimatedAddBuffTo,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(EVC_BeEstimatedAddBuffTo,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(EVC_BeEstimatedAddBuffTo,m_vBuffTypeIncludes,std::vector<int32>()).Editor_SetAsForeignKeySet(eDefGroup_Default,eDefaultDef_ReactorTypes); 
		REGISTER_REFLECTION_MEMBER(EVC_BeEstimatedAddBuffTo,m_vBuffTypeExcludes,std::vector<int32>()).Editor_SetAsForeignKeySet(eDefGroup_Default,eDefaultDef_ReactorTypes); 

		REGISTER_REFLECTION_MEMBER(EVC_BeEstimated,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(EVC_BeEstimated,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(EVC_BeEstimated,m_eResultMatch,eEstimateResult_Hit).Editor_SetAsEnumation<EEstimateResult>();
		

		REGISTER_EVENTCONDITON_MGR(EVC_AfterTakenDamage);
		REGISTER_EVENTCONDITON_MGR(EVC_AfterMeleeAttackOneTarget);
		REGISTER_EVENTCONDITON_MGR(EVC_BeCaculateDamageLimit);
		REGISTER_EVENTCONDITON_MGR(EVC_BeCaculateDamageReduce);
		REGISTER_EVENTCONDITON_MGR(EVC_BeEstimatedAddBuffTo);
		REGISTER_EVENTCONDITON_MGR(EVC_BeEstimated);
	
	}
}___eventcontitiondefreflectionregister;


bool EVC_BeEstimatedAddBuffTo::ConditonCheck( EventArgBase& rEventArgs ) const
{
	if(rEventArgs.m_eEvent!=MatchEventID) return false;
	if(!m_vBuffTypeIncludes.empty())
	{
		return Util::RangeCheckID(true,TrueType(rEventArgs).m_pBuffToAdd->m_iTypeID,m_vBuffTypeIncludes);
	}
	if(!m_vBuffTypeExcludes.empty())
	{
		return Util::RangeCheckID(false,TrueType(rEventArgs).m_pBuffToAdd->m_iTypeID,m_vBuffTypeExcludes);
	}
	return true;

}


bool EVC_AfterMeleeAttackOneTarget::ConditonCheck( EventArgBase& rEventArgs ) const
{
	if(m_tiFirstAttackOrNot!=eTriple_Null)
	{
		if(m_tiFirstAttackOrNot==eTriple_True) 
		{
			if(!TrueType(rEventArgs).m_bFirstAttack) 
			{
				return false;
			}
		}
		else
		{
			if(TrueType(rEventArgs).m_bFirstAttack) 
			{
				return false;
			}
		}
	}
	if(m_iHpChangeGreaterThan!=INVALID_ID)
	{
		if(abs(TrueType(rEventArgs).m_iHpChange)<=m_iHpChangeGreaterThan) return false;
	}
	if(m_iDamageGreaterThan!=INVALID_ID)
	{
		if(abs(TrueType(rEventArgs).m_iDamage)<=m_iDamageGreaterThan) return false;
	}
	return true;

}


bool EVC_AfterTakenDamage::ConditonCheck( EventArgBase& rEventArgs ) const
{
	if(!m_vDmgTypeIncludes.empty())
	{
		return Util::RangeCheckID(true,TrueType(rEventArgs).m_eDamageType,m_vDmgTypeIncludes);
	}
	if(!m_vDmgTypeExcludes.empty())
	{
		return Util::RangeCheckID(false,TrueType(rEventArgs).m_eDamageType,m_vDmgTypeExcludes);
	}
	return true;
}



bool EVC_BeCaculateDamageReduce::ConditonCheck( EventArgBase& rEventArgs ) const
{
	if(!m_vDmgTypeIncludes.empty())
	{
		return Util::RangeCheckID(true,TrueType(rEventArgs).m_eDamageType,m_vDmgTypeIncludes);
	}
	if(!m_vDmgTypeExcludes.empty())
	{
		return Util::RangeCheckID(false,TrueType(rEventArgs).m_eDamageType,m_vDmgTypeExcludes);
	}
	if(m_eDmgCompare!=eCompare_Null)
	{
		switch(m_eDmgCompare)
		{
		case eCompare_Equal: 
			{
				return (TrueType(rEventArgs).m_iDamage==m_iDmgCompareValue);
			}
		case eCompare_Greater:
			{
				return  (TrueType(rEventArgs).m_iDamage>m_iDmgCompareValue);
			}
		case eCompare_Less:
			{
				return  (TrueType(rEventArgs).m_iDamage<m_iDmgCompareValue);
			}
		default:
			return false;
		}
	}
	return true;
}
bool EVC_BeCaculateDamageLimit::ConditonCheck( EventArgBase& rEventArgs ) const
{
	if(!m_vDmgTypeIncludes.empty())
	{
		return Util::RangeCheckID(true,TrueType(rEventArgs).m_eDamageType,m_vDmgTypeIncludes);
	}
	if(!m_vDmgTypeExcludes.empty())
	{
		return Util::RangeCheckID(false,TrueType(rEventArgs).m_eDamageType,m_vDmgTypeExcludes);
	}
	return true;
}

EventConditionBase::~EventConditionBase()
{
}

bool EVC_BeEstimated::ConditonCheck( EventArgBase& rEventArgs ) const
{
	return m_eResultMatch== TrueType(rEventArgs).m_eResult;
}